/****************************************************************************
  From Qt Examples
****************************************************************************/

#include <QtGui>
#include <QtNetwork>

#include <stdlib.h>

#include "dialog.h"
#include "proxy.h"
#include "ui_tracker.h"
#include "ui_serverload.h"

#include "serverload.h"

class QThread;

Dialog::Dialog(QWidget *parent)
    : QDialog(parent),
      ui(new Ui::Dialog)
{
    ui->setupUi(this);

    QWidget *scrollAreaWidgetContents = new QWidget();
    layout = new QVBoxLayout(scrollAreaWidgetContents);


    ui->scrollArea->setWidget(scrollAreaWidgetContents);

    /************************************************************************/
    ui->quitButton->setAutoDefault(false);

    quint16 portNumber = 5001;
    if (!proxy.listen(QHostAddress::Any,portNumber)) {
        QMessageBox::critical(this, tr("Proxy Server"),
                              tr("Unable to start the server: %1.")
                              .arg(proxy.errorString()));
        close();
        return;
    }


    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    // use the first non-localhost IPv4 address
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
            ipAddressesList.at(i).toIPv4Address()) {
            ipAddress = ipAddressesList.at(i).toString();
            break;
        }
    }

    // if we did not find one, use IPv4 localhost
    if (ipAddress.isEmpty())
        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    ui->statusLabel->setText(tr("The Proxy Server is running on\n\nIP: %1\nport: %2\n\n"
                            "Run the Server(s).")
                         .arg(ipAddress).arg(proxy.serverPort()));

    //SIGNALS/SLOTS
    connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(close()));
    //connect(serverLineEdit,SIGNAL(returnPressed()),this,SLOT(send()));
    connect((&proxy),SIGNAL(transferMessage(QString)),this,SLOT(readCMsg(QString)));
    connect(&proxy, SIGNAL(serverAdded(ServerLoad*, void*)), this, SLOT(serverAdded(ServerLoad*, void*)));

    connect(&proxy, SIGNAL(setAverageWorkload(int,int)), this, SLOT(setAverageWorkload(int,int)));
    connect(&proxy, SIGNAL(serverRemoved(ServerLoad*)), this, SLOT(serverRemoved(ServerLoad*)));
    connect(ui->startButton, SIGNAL(clicked()), &proxy, SLOT(startSim()));
    connect(ui->stopButton, SIGNAL(clicked()), &proxy, SLOT(stopSim()));

    setWindowTitle(tr("Proxy Server"));

    //tessting
    //connectionD = new QTcpSocket();
}

Dialog::~Dialog() {
    delete ui->proxyTextEdit;
    delete ui->statusLabel;
    delete ui->quitButton;

    delete ui;
}

void Dialog::readCMsg(QString msg)
{
    qDebug() << "MSG Recived!!: " << msg;
    ui->proxyTextEdit->append(msg);
}

void Dialog::serverAdded(ServerLoad * server, void*)
{
    layout->addWidget(server);
}

void Dialog::serverRemoved(ServerLoad *server)
{
    layout->removeWidget(server);
}

void Dialog::setAverageWorkload(int count, int sum)
{

    ui->averageBar->setValue(sum/count);

    //set number of servers
    ui->totalNumOfServers->setText(QString::number(count));
}
